package com.miyake.dsp.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;


public class LineChartPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5224105815155167578L;
	private ChartPanel panel;
	private double xCoeff = 1;
	private final XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
	private XYSeriesCollection xySeriesCollectionOverlay;
	
	public LineChartPanel(String title) {		
		JFreeChart chart = ChartFactory.createXYLineChart(title, "X", "Y", 
				xySeriesCollection, PlotOrientation.VERTICAL, true, false, false);
		panel = new ChartPanel(chart);
		panel.setPreferredSize(new Dimension(400,300));
		//panel.setSize(300, 200);
//		this.setSize(400, 200);
		this.add(panel,BorderLayout.CENTER);
		
		JButton clearButton = new JButton("Clear");
		clearButton.setVisible(false);
		this.add(clearButton, BorderLayout.SOUTH);
		clearButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				removeAllTrace();
			}
		});
		
		addComponentListener(new ComponentListener() {
			@Override
			public void componentHidden(ComponentEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void componentMoved(ComponentEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void componentResized(ComponentEvent arg0) {
				panel.setPreferredSize(new Dimension(arg0.getComponent().getWidth(),
						arg0.getComponent().getHeight()));
			}

			@Override
			public void componentShown(ComponentEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
	}

	public synchronized void setData(String title, double[] x, double[] y) {
		XYSeries xy = new XYSeries(title);
		for (int i = 0; i < y.length; i++) {
			xy.add(x[i], y[i]);
		}
		xySeriesCollection.addSeries(xy);	
	}
	
	public synchronized void setData(String title, double[] y) {
		if (y == null) {
			return;
		}
		
		XYSeries xy = new XYSeries(title);
		for (int i = 0; i < y.length; i++) {
			xy.add(i * xCoeff, y[i]);
		}
		xySeriesCollection.addSeries(xy);	
	}
	
	public synchronized void removeAllTrace() {
		xySeriesCollection.removeAllSeries();
	}
	
	public static void showChart(double[] d, String title) {
		JFrame f = new JFrame();
		LineChartPanel chart = new LineChartPanel(title);
		chart.setData("", d);
		f.getContentPane().add(chart);
		f.setVisible(true);
		f.setSize(new Dimension(400,300));
	}

	public double getxCoeff() {
		return xCoeff;
	}

	public void setxCoeff(double xCoeff) {
		this.xCoeff = xCoeff;
	}
	
	public int getXPoints() {
		return xySeriesCollection.getSeries(0).getItemCount();
	}
	
	public void addMarker(List<Double> markers) {
		/*
		if (xySeriesCollection.getSeriesCount() == 0) {
			return;
		}
		XYSeries trace = xySeriesCollection.getSeries(0);
		XYSeries xy = new XYSeries("");
		for (Integer d : markers) {
			xy.add(trace.getX(d.intValue()), trace.getY(d.intValue()));
		}
		xySeriesCollection.addSeries(xy);
		*/
		panel.getChart().getXYPlot().clearDomainMarkers();
		for (Double d : markers) {
			ValueMarker marker = new ValueMarker(d.doubleValue());
			marker.setPaint(Color.blue);
			panel.getChart().getXYPlot().addDomainMarker(marker);
		}
	}
}
